package edu.uwm.capstone.diasmart.types;

import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.widget.EditText;
import android.widget.TableRow;
import edu.uwm.capstone.diasmart.R;
import edu.uwm.capstone.diasmart.database.DatabaseHelper;
import edu.uwm.capstone.diasmart.database.Entry;
import edu.uwm.capstone.diasmart.database.SubType;

public class BPType implements EditableEntryType {
	private static final char[] BP_EDIT_SEPARATORS = { 45, 32, 47 };
	private static final char BP_SEPARATOR = '/';

	@Override
	public String getEditableValue(EntryContext entryContext, double value) {
		short[] arrayOfShort = toShortValues((int) value);
		return "" + arrayOfShort[0] + BP_EDIT_SEPARATORS[0] + arrayOfShort[1];
	}

	private short[] toShortValues(int paramInt) {
		short[] arrayOfShort = new short[2];
		arrayOfShort[1] = ((short) paramInt);
		arrayOfShort[0] = ((short) (paramInt >>> 16));
		return arrayOfShort;
	}

	@Override
	public String getFormattedValue(EntryContext entryContext, double value) {
		short[] arrayOfShort = toShortValues((int) value);
		return "" + arrayOfShort[0] + BP_SEPARATOR + arrayOfShort[1];
	}

	@Override
	public String getMeasurementUnit(EntryContext entryContext) {
		return entryContext.getContext().getString(
				R.string.systolic_measurement_name)
				+ BP_SEPARATOR
				+ entryContext.getContext().getString(
						R.string.diastolic_measurement_name);
	}

	@Override
	public String getMeasurementUnit(EntryContext entryContext, SubType subType) {
		if ((subType == null) || (subType.getUnit() == null))
			return getMeasurementUnit(entryContext);
		return subType.getUnit();
	}

	@Override
	public int getType() {
		return 5;
	}

	@Override
	public double toActualValue(EntryContext paramEntryContext, long paramLong) {

		return (double) paramLong;
	}

	public int toIntValue(short paramShort1, short paramShort2) {
		return paramShort1 << 16 | paramShort2 & 0xFF;
	}

	@Override
	public long toStoredValue(EntryContext paramEntryContext, double paramDouble) {
		return (long) paramDouble;
	}

	@Override
	public double toValue(EntryContext paramEntryContext, String paramString) {
		int i = -1;
		for (int j = 0; j < BP_EDIT_SEPARATORS.length; j++) {

			do {
				i = paramString.indexOf(BP_EDIT_SEPARATORS[j]);
			} while (i >= 0);
		}
		return toIntValue(Short.valueOf(paramString.substring(0, i))
				.shortValue(), Short.valueOf(paramString.substring(i + 1))
				.shortValue());
	}

	@Override
	public String getKeyboardType() {
		return "phone";
	}

	@Override
	public TableRow[] getUserInterface(Activity activity) {
		return EditableEntryTypeHelper.getStandardUserInterface(activity, this,
				R.id.edtBPValue, R.id.sprBPSubType);
	}

	@Override
	public boolean handleOnActivityResult(Activity activity, int paramInt1,
			int paramInt2, Intent paramIntent) {
		return EditableEntryTypeHelper
				.handleOnActivityResult(activity, getType(), R.id.sprBPSubType,
						paramInt1, paramInt2, paramIntent);
	}

	@Override
	public long save(DatabaseHelper paramDatabaseHelper, Entry paramEntry,
			View paramView) {
		return EditableEntryTypeHelper.saveStandard(paramDatabaseHelper, this,
				paramEntry, paramView, R.id.edtBPValue, R.id.sprBPSubType);
	}

	@Override
	public int validate(View paramView) {
		EditText localEditText = (EditText) paramView
				.findViewById(R.id.edtBPValue);
		return validate(new EntryContext(paramView.getContext()), localEditText
				.getText().toString());
	}

	@Override
	public int validate(EntryContext paramEntryContext, String paramString) {
		try {
			toValue(paramEntryContext, paramString);
			return 0;
		} catch (Exception localException) {
		}
		return R.string.invalid_bp_value;
	}

	public String toString() {
		return "Blood Pressure";
	}
}
